import { ILoginData, loginApi } from '@/apis/userApi';
import { CacheEnum } from '@/enum/cacheEnum';
import userStore from '@/store/user-store';
import storage from './storage';
import router from '@/router';

export async function login(values: ILoginData) {
  const {
    result: { token }
  } = await loginApi(values);
  storage.set(CacheEnum.TOKEN_NAME, { token });
  const routeName = storage.get(CacheEnum.REDIRECT_ROUTE_NAME) ?? 'home';
  router.push({ name: routeName });
}

export function logout() {
  storage.remove(CacheEnum.TOKEN_NAME);
  userStore().info = null;
}
